package org.game.module.event.listener;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Sets;
import org.apache.commons.collections4.MapUtils;
import org.base.BaseGoods;
import org.cache.GoodsCfgCache;
import org.cfg.EquipmentCfg;
import org.cfg.EquipmentSuitCfg;
import org.cfg.cache.EquipmentSuitCfgCache;
import org.constant.BagType;
import org.constant.EventConditionKey;
import org.constant.EventConditionType;
import org.constant.EventType;
import org.game.db.bean.Item;
import org.game.db.bean.Player;
import org.game.db.bean.Role;
import org.game.db.cache.RoleCache;
import org.game.db.vo.BackpackVO;
import org.game.db.vo.EquipmentVO;
import org.game.manager.EquipmentManager;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventListener;
import org.game.plugin.event.GameEventStack;
import org.game.plugin.event.GameEventType;

import com.sojoys.artifact.tools.ToolMap;

/**
 * 时装事件监听器
 * Created by fxf on 2017/11/17.
 */
@GameEventType(EventType.EQUIPMENT)
public class EquipEventListener implements GameEventListener {
    @Override
    public void onEvent(GameEvent event) {
        Player player = event.getPlayer();
        GameEventStack eventStack = event.peek();
        switch (eventStack.getConditionType()) {
            //某看板娘第一次穿某时装
            case EventConditionType.DRESS:{
            	if (checkTriggerType(event.getIn(), eventStack.getConditionType())) {
            		boolean bool = true;
            		bool &= verify_eq(EventConditionKey.ROLE_CID, event.getIn(), eventStack.getParams());
                    bool &= verify_eq(EventConditionKey.DRESS_ID, event.getIn(), eventStack.getParams());
                    
                    boolean checkCount = false;
    				if(ToolMap.getBoolean(EventConditionKey.FIRST_TIME,eventStack.getParams(),false)){
    					checkCount = isFirstTimeDress(player, event, eventStack);		
    					bool &= checkCount;
    				}

                    if (bool) {
                        eventStack.setProgress(1);
                        if (checkCount) {
                        	updateRole(player,event);							
						}
                    }
            	}
                break;
            }
             // 灵装最大等级
            case EventConditionType.EQUIP_MAX_LVL:{
                if (checkTriggerType(event.getIn(), eventStack.getConditionType())) {
                	boolean bool = verify_eq(EventConditionKey.EQUIPMENT_CID,event.getIn(), eventStack.getParams());
                    int equipLvl = ToolMap.getInt(EventConditionKey.NOW_LEVEL, event.getIn());
                    int progress = eventStack.getMaxProgress();
                    if(bool && equipLvl <= progress) {
        				eventStack.setProgress(equipLvl);
        			}
				}
                break;
            }
            // 灵装套装数量

            default:
                break;
        }
    }

    @Override
    public void initEventCondition(Player player, GameEventStack eventStack) {
    	switch (eventStack.getConditionType()) {
        //某看板娘第一次穿某时装
        case EventConditionType.DRESS:{
            break;
        }
         // 灵装最大等级
        case EventConditionType.EQUIP_MAX_LVL:{
        	int equipId = ToolMap.getInt(EventConditionKey.EQUIPMENT_CID, eventStack.getParams());
        	int maxLvl = player.getPlayerProxy().getMaxEquipLvl(equipId);
			eventStack.setProgress(maxLvl);
            break;
        }
        case EventConditionType.EQUIP_COVER_COUNT:{
        	int coverId = ToolMap.getInt(EventConditionKey.COVER_ID, eventStack.getParams());
        	BackpackVO backpackVO = player.getPlayerProxy().getBackpackByType(BagType.EQUIPMENT);
    		Collection<Item> equips = backpackVO.getAll();
    		int maxLvl = 0;
    		Set<Integer> set = new HashSet<>();
    		for (Item item : equips) {
    			EquipmentVO equip = (EquipmentVO) item;
    			if (!(item instanceof EquipmentVO)) {
    				continue;
    			}
    			if (equip.getCfg().getSuit() != coverId || set.contains(coverId)) {
    				continue;
    			}
    			if(EquipmentManager.me().checkHaveCover(player, coverId)){
    				maxLvl++;
    				set.add(coverId);
    			}
    		}
			eventStack.setProgress(maxLvl);
            break;
        }
        default:
            break;
    }
    }

    /**
     * 是否第一次穿次时装
     */
    private boolean isFirstTimeDress(Player player, GameEvent event, GameEventStack eventStack){
    	boolean bool = MapUtils.getBoolean(event.getFirstMap(), "isFirstTimeDress",false);
    	if (bool) {
			return bool;
		}
    	
        Role role = RoleCache.me().getByCidPlayerId((Integer) event.getIn().get(EventConditionKey.ROLE_CID),player.getId());
        int dressCid = (int) event.getIn().get(EventConditionKey.DRESS_ID);
        bool = !role.getDressDating().contains(dressCid);
        if (bool) {
        	event.getFirstMap().put("isFirstTimeDress", true);
		}
        return bool;
    }

    private void updateRole(Player player, GameEvent event){
    	boolean bool = MapUtils.getBoolean(event.getFirstMap(), "updateRole",false);
    	if (bool) {
			return;
		}
        Role role = RoleCache.me().getByCidPlayerId((Integer) event.getIn().get(EventConditionKey.ROLE_CID),player.getId());
        int dressCid = (int) event.getIn().get(EventConditionKey.ROLE_CID);
        role.getDressDating().add(dressCid);
        role.update();
        
        event.getFirstMap().put("updateRole", true);
    }

}
